package com.xinbochuang.machine.admin.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinbochuang.machine.admin.domain.GejieOrder;
import com.xinbochuang.machine.admin.domain.Macinout;
import com.xinbochuang.machine.admin.domain.ShigongOrder;
import com.xinbochuang.machine.admin.domain.system.SysUser;
import com.xinbochuang.machine.admin.service.IGejieOrderService;
import com.xinbochuang.machine.admin.service.IMacinoutService;
import com.xinbochuang.machine.common.controller.BaseController;
import com.xinbochuang.machine.common.domain.BaseEntity;
import com.xinbochuang.machine.common.domain.LoginUser;
import com.xinbochuang.machine.common.domain.TableDataInfo;
import com.xinbochuang.machine.common.utils.ServletUtils;
import com.xinbochuang.machine.framework.web.domain.AjaxResult;
import com.xinbochuang.machine.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 杜文博
 * @date 2020-10-26
 */
@RestController
@RequestMapping("/gejie-order")
public class GejieOrderController extends BaseController {

    @Autowired
    private IGejieOrderService gejieOrderService;

    @Autowired
    private IMacinoutService iMacinoutService;

    @Resource
    private TokenService tokenService;
    @GetMapping("/detail")
    public AjaxResult getGejieDetail(@RequestParam("uuid") String uuid) {
        return AjaxResult.data(gejieOrderService.getGejieDetail(uuid));
    }

    @GetMapping("/getAllGejieOrder")
    public AjaxResult getAllGejieOrder(@RequestParam("query") String query) {

        QueryWrapper<GejieOrder> queryWrapper = new QueryWrapper<>();
        if(!query.equals("")&&!query.equals("undefined")){
            queryWrapper.lambda().like(GejieOrder::getGjOrderName,query);
        }

//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        SysUser user = loginUser.getUser();
//        String deptName = user.getDept().getDeptName();
//        if (StrUtil.isNotBlank(deptName)) {
//            queryWrapper.lambda().like(GejieOrder::getGjFreeDept,deptName);
//        }
        queryWrapper.lambda().eq(GejieOrder::getDeleteFlag, 0);
        queryWrapper.lambda().ge(GejieOrder::getGjWorkEndTime, DateUtil.today());
        queryWrapper.notInSql("uuid","select order_id from macinout where order_id is not null group by order_id");
        queryWrapper.orderByAsc("gj_work_begin_time");
        return AjaxResult.data(gejieOrderService.list(queryWrapper));
    }

    /**
     * 查询分页列表
     */
    @GetMapping("/list")
    public TableDataInfo getList(GejieOrder order) {
        QueryWrapper<GejieOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(BaseEntity::getCreateTime);
        if (StrUtil.isNotBlank(order.getGjOrderName())) {
            queryWrapper.lambda().like(GejieOrder::getGjOrderName, order.getGjOrderName());
        }
        if (StrUtil.isNotBlank(order.getGjCreateOrderUserName())) {
            queryWrapper.lambda().like(GejieOrder::getGjCreateOrderUserName, order.getGjCreateOrderUserName());
        }
        return getDataTable(gejieOrderService.page(new Page<>(order.getPageNum(), order.getPageSize()), queryWrapper));
    }

}

